﻿using System;

using System.Collections.Generic;
using System.Text;

namespace Silvermoon.Controls.Classes
{
    /// <summary>
    /// Defines rows columns and cells for a <see cref="T:Grid"/>.
    /// Separating the definition from the actual grid saves memory and cpu time by allowing static definitions to be
    /// reused for instances of a grid.
    /// </summary>
    public class GridDefinition
    {
        public GridDefinition()
            : base()
        {
            Rows = new GridUnitCollection();
            Columns = new GridUnitCollection();
            Cells = new List<GridCell>();
        }

        /// <summary>
        /// Gets the collection of rows to define.
        /// </summary>
        public GridUnitCollection Rows { get; private set; }

        /// <summary>
        /// Gets the collection of columns to define.
        /// </summary>
        public GridUnitCollection Columns { get; private set; }

        /// <summary>
        /// Gets the collection of cells to define.
        /// </summary>
        public List<GridCell> Cells { get; private set; }

        /// <summary>
        /// Creates a new cell.
        /// </summary>
        /// <param name="col">The column the cell. </param>
        /// <param name="row">The row of the cell.</param>
        /// <returns>A new Cell.</returns>
        public GridCell CreateCell(int col, int row)
        {
            GridCell cell = new GridCell { Row = row, Col = col };
            Cells.Add(cell);
            return cell;
        }


        /// <summary>
        /// Creates a new cell.
        /// </summary>
        /// <param name="col">The column the cell. </param>
        /// <param name="row">The row of the cell.</param>
        /// <param name="colSpan">Specifies how many columns to span.</param>
        /// <param name="rowSpan">Specifies how many rows to span.</param>
        /// <returns>A new Cell.</returns>
        public GridCell CreateCell(int col, int row, int colSpan, int rowSpan)
        {
            GridCell cell = new GridCell { Row = row, Col = col, ColSpan = colSpan, RowSpan = rowSpan };
            Cells.Add(cell);
            return cell;
        }

        public void CreateAutoCells()
        {
            Cells.Clear();
            for (int row = 0; row < Rows.Count; row++)
            {
                for (int col = 0; col < Columns.Count; col++)
                {
                    GridCell cell = new GridCell { Row = row, Col = col };
                    Cells.Add(cell);
                }
            }
        }
    }
}
